package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsUserChargeTypeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsUserTypeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.CustomerRechargeMapper;
import com.caiyi.financial.nirvana.sms.mapper.SmsMsgMapper;
import com.caiyi.financial.nirvana.sms.mapper.SmsUserMapper;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.caiyi.financial.nirvana.sms.util.Constant;
import com.caiyi.financial.nirvana.sms.util.MD5Util;
import com.caiyi.financial.nirvana.sms.util.ValidateUtil;
import com.youyu.common.api.Result;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SmsCustomerService extends BaseService<SmsUser> {

    @Autowired
    private SmsUserMapper smsUserMapper;

    @Autowired
    private SmsMsgMapper smsMsgMapper;

    @Autowired
    private CustomerRechargeMapper customerRechargeMapper;

    @Autowired
    private Environment env;


    /**
     * 根据账号查询用户
     *
     * @param account 账号
     * @return 管理后台用户
     */
    public SmsUser getSmsUser(String account) {
        return smsUserMapper.getSmsUser(account);
    }

    /**
     * 修改客户资料
     *
     * @param user
     * @return
     */
    public int updateSmsUser(SmsUser user) {
        if (ValidateUtil.isNotNll(user.getRemindPhone()) && !ValidateUtil.isPhone(user.getRemindPhone())) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "电话号码格式有误！");
        }
        if (ValidateUtil.isNotNll(user.getReceiveUrl())) {
            if (ValidateUtil.isLength(user.getReceiveUrl(), 512)) {
                YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "上行推送地址最大长度为512。");
            }
            if (!ValidateUtil.isUrl(user.getReceiveUrl())) {
                YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "上行推送地址格式有误！");
            }
        }
        if (user.getRemindValue() != null && user.getRemindValue() > 10000000) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "余额提醒条数不能超过10000000");
        }
        if (ValidateUtil.isNotNll(user.getIp())) {
            if (ValidateUtil.isLength(user.getIp(), 255)) {
                YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "ip地址不能超过255个字符！");
            }
            String[] ips = user.getIp().split(",");
            for (String ip : ips) {
                YyAssert.isFalse(!ValidateUtil.isIp(ip), ReturnCodeEnum.PARAMS_ERROR.code, "字符串中有IP格式不正确！");
            }
        }
        String key = String.format(JedisKey.SMS_USER, user.getAccount());
        JedisService.set(key, JSON.toJSONString(user));
        return updateByEntitySelective(user);
    }


    public String resetPwd(String account) {
        String password = MD5Util.encode(new Date().toString());
        smsUserMapper.resetPwd(account, password);
        return password;
    }

    public Map index(SmsUser user) {
        Map resultMap = new HashMap();
        //获取账户信息
        user = smsUserMapper.getSmsUser(user.getAccount());
        resultMap.put("extendCode", user.getExtendCode());
        resultMap.put("smsNum", user.getSmsNum());
        resultMap.put("account", user.getAccount());
        resultMap.put("password", user.getPassword());
        resultMap.put("type", getTypeDesc(user.getType()));
        resultMap.put("chargeType", getChargeTypeDesc(user.getChargeType()));
        resultMap.put("receiveUrl", CommUtil.getSendUrl(env.getActiveProfiles()[0]));
        //获取首页统计
        Map countMap = smsMsgMapper.getMsgByDayByAccount(user.getAccount());
        resultMap.put("msgCount", countMap != null ? countMap.get("msgCount") : "");
        resultMap.put("successRate", countMap != null ? countMap.get("successRate") : "");
        resultMap.put("failureRate", countMap != null ? countMap.get("failureRate") : "");
        resultMap.put("unknownRate", countMap != null ? countMap.get("unknownRate") : "");
        resultMap.put("successCount", countMap != null ? countMap.get("successCount") : "");
        resultMap.put("failureCount", countMap != null ? countMap.get("failureCount") : "");
        resultMap.put("unknownCount", countMap != null ? countMap.get("unknownCount") : "");
        //获取首页累积充值条数
        Integer totalSmsNum = customerRechargeMapper.getAccountSmsNum(user.getAccount());
        resultMap.put("totalSmsNum", totalSmsNum == null ? 0 : totalSmsNum);
        //返回接口文档地址和联系QQ
        resultMap.put("QQ", Constant.CUSTOMER_QQ);
        resultMap.put("docUrl", Constant.CUSTOMER_DOC_URL);
        return resultMap;
    }

    public String getChargeTypeDesc(Integer code) {
        for (SmsUserChargeTypeEnum smsUserChargeTypeEnum : SmsUserChargeTypeEnum.values()) {
            if (code.equals(smsUserChargeTypeEnum.index)) {
                return smsUserChargeTypeEnum.desc;
            }
        }
        return null;
    }

    public String getTypeDesc(Integer code) {
        for (SmsUserTypeEnum smsUserTypeEnum : SmsUserTypeEnum.values()) {
            if (code.equals(smsUserTypeEnum.index)) {
                return smsUserTypeEnum.desc;
            }
        }
        return null;
    }
}
